package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private RegionRepository regionRepository;
    @Override
    public void save(Destination user) {
        user.setId(null);
        destinationRepository.save(user);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void update(Destination user) {
        destinationRepository.save(user);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> queryDestinationByRegionId(String id) {
        if (!StringUtils.hasLength(id)){
            return null;
        }
        //通过区域id查询出对应的目的地id集合
        Region region = regionService.get(id);
        //根据目的地集合id查出对应的目的地对象
        List<Destination> list = destinationRepository.findByIdIn(region.getRefIds());
        return list;
    }

    @Override
    public Page<Destination> query(DestinationQueryObject qo) {
        //创建查询对象
        Query query=new Query();
        //增加条件查询,
        if(qo.getKeyword()!=null){
            //关键字查询
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if(StringUtils.hasLength(qo.getParentId())){
            //父id查询
            query.addCriteria(Criteria.where("parentId").regex(qo.getParentId()));
        }
        //查询总数据量
        long count = template.count(query, Destination.class);
        if(count==0){
            return Page.empty();
        }
        //创建分页数据封装对象
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        //分页条件添加到查询对象上
        query.with(pageable);
        //执行查询
        List<Destination> list = template.find(query, Destination.class);


        return new PageImpl<>(list,pageable,count);
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        //判空
        if(!StringUtils.hasLength(parentId)){
            return null;
        }
        List<Destination> list=new ArrayList<>();
        //根据id查询当前目的地对象
        Destination destination = this.get(parentId);
        //添加到集合中
        list.add(destination);
        //判断当前对象是否具有parentId
        while(destination.getParentId()!=null){
            //根据parentId查出目的地对象,添加到list中
            destination=this.get(destination.getParentId());
            list.add(destination);
        }
        //反转list
        Collections.reverse(list);
        return list;
    }
    //递归完成
    public List<Destination> getList(List<Destination> list,String parentId) {
        //判空
        if(!StringUtils.hasLength(parentId)){
            return null;
        }
        //根据id查询当前目的地对象
        Destination destination = this.get(parentId);
        //添加到集合中
        list.add(destination);
        //判断目的地对象是否具有parentId
        if(destination.getParentId()!=null){
            getList(list,destination.getParentId());
        }
        //反转list
        Collections.reverse(list);
       return list;
    }

    @Override
    public List<Region> queryForHost() {
        return regionRepository.findByIshotOrderBySequence(1);
    }

    @Override
    public List<Destination> queryForParentName() {
        return destinationRepository.findByParentName("中国");
    }

    @Override
    public List<Destination> findByParentId(String id) {
        return destinationRepository.findByParentId(id);
    }
}
